package com.stock.project.system.stockorders.controller;

import com.stock.common.utils.poi.ExcelUtil;
import com.stock.framework.aspectj.lang.annotation.Log;
import com.stock.framework.aspectj.lang.enums.BusinessType;
import com.stock.framework.web.controller.BaseController;
import com.stock.framework.web.domain.AjaxResult;
import com.stock.framework.web.page.TableDataInfo;
import com.stock.project.system.stockorders.domain.StockOrders;
import com.stock.project.system.stockorders.service.IStockOrdersService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存订单 信息操作处理
 *
 * @author stock
 * @date 2019-03-14
 */
@Controller
@RequestMapping("/system/stockOrders")
public class StockOrdersController extends BaseController {
    private String prefix = "system/stockOrders";

    @Autowired
    private IStockOrdersService stockOrdersService;

    @RequiresPermissions("system:stockOrders:view")
    @GetMapping()
    public String stockOrders() {
        return prefix + "/stockOrders";
    }

    /**
     * 查询库存订单列表
     */
    @RequiresPermissions("system:stockOrders:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StockOrders stockOrders) {
        startPage();
        List<StockOrders> list = stockOrdersService.selectStockOrdersList(stockOrders);
        return getDataTable(list);
    }


    /**
     * 导出库存订单列表
     */
    @RequiresPermissions("system:stockOrders:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StockOrders stockOrders) {
        List<StockOrders> list = stockOrdersService.selectStockOrdersList(stockOrders);
        ExcelUtil<StockOrders> util = new ExcelUtil<StockOrders>(StockOrders.class);
        return util.exportExcel(list, "stockOrders");
    }

    /**
     * 新增库存订单
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存库存订单
     */
    @RequiresPermissions("system:stockOrders:add")
    @Log(title = "库存订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StockOrders stockOrders) {
        int result = stockOrdersService.insertStockOrders(stockOrders);
        if(0 == result){
            return error("销售出库单商品不存在");
        }else if (-1 == result){
            return error("销售出库单库存不足");
        }else {
            return toAjax(result);
        }
    }

    /**
     * 修改库存订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        StockOrders stockOrders = stockOrdersService.selectStockOrdersById(id);
        mmap.put("stockOrders", stockOrders);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存订单
     */
    @RequiresPermissions("system:stockOrders:edit")
    @Log(title = "库存订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StockOrders stockOrders) {
        return toAjax(stockOrdersService.updateStockOrders(stockOrders));
    }

    /**
     * 删除库存订单
     */
    @RequiresPermissions("system:stockOrders:remove")
    @Log(title = "库存订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(stockOrdersService.deleteStockOrdersByIds(ids));
    }

}
